<?php

/**
 * Facebook Plugin
 * A generic Facebook plugin for CakePHP : <http://www.cakephp.org/>
 * 
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @filesource
 * @copyright		Copyright (c)	2009, Michael DePuy.
 * @link			http://www.apswerve.com
 * @package			facebook-plugin
 * @license			http://www.opensource.org/licenses/mit-license.php The MIT License
 */

App::import('Vendor', 'facebook'); 
App::import('Vendor', 'echove');

// set Manager version
DEFINE('FACEBOOK_VERSION','0.1');

// load config
$config_file = 'facebook_config.php';
if(file_exists(dirname(__FILE__).DS.$config_file)) {
	require_once($config_file);
} else {
	exit('<pre>Please create the file <strong>facebook_config.php and update the $config_file variable</strong>...</pre>');
}

/**
 * Application controller for Facebook.
 */
class FacebookAppController extends AppController {
	// Use the FacebookAction model to store user activity
	var $uses = array('FacebookAction','BrightcoveVideo');
	// Use this array to store actions.
	// Actions stored in this array will be stored in the database in the afterFilter() method
	var $facebook_actions = array();
	
	var $facebook;
	var $echove;
	
    var $__fbApiKey;
    var $__fbSecret;
	
    function __construct() {
		$init=parse_ini_file('/home/aptana/homes/mdepuy/public/web/config.php');
		$this->__fbApiKey = FACEBOOK_API_KEY;
		$this->__fbSecret  = FACEBOOK_SECRET;
		
		$this->set('app_root_url', FACEBOOK_CANVAS_URL);
		$this->set('app_name', FACEBOOK_APPLICATION_NAME);
		$this->set('app_id', FACEBOOK_APPLICATION_ID);
		$this->set('app_description', FACEBOOK_APPLICATION_DESCRIPTION);
		
		$this->set('default_product_plural', Inflector::humanize(Inflector::underscore(Inflector::pluralize(FACEBOOK_DEFAULT_PRODUCT))));
		$this->set('default_product_singular', Inflector::humanize(Inflector::underscore(Inflector::singularize(FACEBOOK_DEFAULT_PRODUCT))));
		$path = $init['path'];
    	parent::__construct();  
   
        // Prevent the 'Undefined index: facebook_config' notice from being thrown.  
        $GLOBALS['facebook_config']['debug'] = NULL;  
   
        // Create a Facebook client API object.  
        $this->facebook = new Facebook($this->__fbApiKey, $this->__fbSecret);
		
		// Create a Brightcove client using echove
		$this->echove = new Echove(BRIGHTCOVE_SECRET);
		
	}
	
	function afterFilter() {
//		print_r($this);
		if(sizeof($this->facebook_actions)) {
			foreach($this->facebook_actions as $a) {
				$this->FacebookAction->create();
				$this->FacebookAction->save($a);
			}
		}
	}
	 
	 
}

?>